Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

needle

Package Overview
Dependencies
Maintainers
1
Versions
112
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

needle

The leanest and most handsome HTTP client in the Nodelands.

  • 2.9.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.7M
decreased by-25.46%
Maintainers
1
Weekly downloads
 
Created

What is needle?

The needle npm package is a minimalistic HTTP client for Node.js, designed for simplicity and ease of use. It provides a straightforward interface for making HTTP requests and handling responses, supporting both callbacks and streams. It is lightweight and has built-in support for multipart form-data, which makes it suitable for file uploads and other form-related tasks.

What are needle's main functionalities?

Making HTTP GET requests

This feature allows you to perform HTTP GET requests to retrieve data from a specified URL. The callback function receives an error object and the response object, which includes the status code and the response body.

const needle = require('needle');

needle.get('https://api.example.com', (error, response) => {
  if (!error && response.statusCode == 200)
    console.log(response.body);
});

Making HTTP POST requests

This feature is used to send HTTP POST requests with data to a specified URL. The data can be an object, which needle will automatically encode as application/x-www-form-urlencoded or multipart/form-data, depending on the content.

const needle = require('needle');

const data = { foo: 'bar' };
needle.post('https://api.example.com', data, (error, response) => {
  if (!error && response.statusCode == 200)
    console.log(response.body);
});

Streaming support

Needle supports streaming, which allows you to pipe the response stream to another stream, such as a file write stream. This is useful for handling large amounts of data or streaming file downloads.

const needle = require('needle');
const fs = require('fs');

const stream = needle.get('https://api.example.com/stream');
stream.pipe(fs.createWriteStream('output.txt'));

Handling multipart form-data

Needle can handle multipart form-data, which is often used for file uploads. The data object can include file buffers, streams, or paths, and needle will handle the multipart encoding for you.

const needle = require('needle');

const data = {
  file: { file: 'path/to/file.jpg', content_type: 'image/jpeg' },
  other_field: 'value'
};

needle.post('https://api.example.com/upload', data, { multipart: true }, (error, response) => {
  if (!error && response.statusCode == 200)
    console.log('Upload successful');
});

Other packages similar to needle

Keywords

FAQs

Package last updated on 01 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc